package so.shanku.essential.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import net.tsz.afinal.json.JsonMap;
import so.shanku.essential.R;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.view.CircleImageView;
import so.shanku.essential.view.NineGridView;
import so.shanku.essential.view.NineGridView.OnImageClickListener;

/**
 * Created by Jaelyn on 16/3/8.
 * 日志数据适配
 * update 2016/4/12
 */
public class BlogAdapter extends MyBaseAdapter{
    ItemCompontClickListener itemCompontClickListener;
    
    private Moment moment ;
    private int flag=1;
    private ArrayList<Moment> moments = new ArrayList<Moment>(9) ;
    
    /**
     * 虚拟数据
     * FangDongzhang
     */
    public static String[] urls = new String[]{
            "http://img.blog.csdn.net/20151115004147583",
            "http://img.blog.csdn.net/20151115004214075",
            "http://img.blog.csdn.net/20151115004236274",
            "http://img.blog.csdn.net/20151115004258230",
            "http://img.blog.csdn.net/20151115004316107",
            "http://img.blog.csdn.net/20151115004348219",
            "http://img.blog.csdn.net/20151115004411358",
            "http://img.blog.csdn.net/20151115004442588",
            "http://img.blog.csdn.net/20151115004502212"
    } ;
    /**
     * 后期替换成后台返回数据，取消在此初始化数据方法
     * FangDongzhang
     */
    private void initData() {

        for (int i=0;i<9;i++) {
            Moment moment = new Moment() ;
            moment.content = "content:" + (i+1) ;

            Integer[] resource = new Integer[(i+1)] ;
            String[] address = new String[(i+1)] ;
            for (int j=0;j<resource.length;j++) {
//                resource[j] = images[j] ;
                address[j] = urls[j] ;
            }

            moment.resource = resource ;
            moment.address = address ;
            moments.add(moment) ;
        }

//        listview = (ListView) findViewById(R.id.listview);
//        listview.setAdapter(new MyAdapter());
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public void setItemCompontClickListener(ItemCompontClickListener itemCompontClickListener) {
        this.itemCompontClickListener = itemCompontClickListener;
    }

    public BlogAdapter(Context context) {
        super(context);
        
        initData();
    }

    public BlogAdapter(Context context, List<? extends Map<String, ?>> datas) {
        super(context, datas);
        
        initData();
    }


    @Override
    public int getCount() {
        return 9;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
    	moment = moments.get(i);
        BlogAdapterViewHolder blogAdapterViewHolder;
        JsonMap jsonMap = new JsonMap();//= (JsonMap) datas.get(i);
        if (view == null) {
            blogAdapterViewHolder = new BlogAdapterViewHolder();
            view = layoutInflater.inflate(R.layout.blog_item, null);
            ViewUtils.inject(blogAdapterViewHolder, view);
            view.setTag(blogAdapterViewHolder);
        } else {
            blogAdapterViewHolder = (BlogAdapterViewHolder) view.getTag();
        }

        setTag(i, blogAdapterViewHolder);
        bindData(i, blogAdapterViewHolder, jsonMap ,moment);
        
        blogAdapterViewHolder.blog_content_image_gv.setOnImageClickListener(new OnImageClickListener() {
			
			@Override
			public void onImageCilcked(int position, View view) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "onItemClick"+position, Toast.LENGTH_SHORT).show();
	            if (itemCompontClickListener != null) {
	                itemCompontClickListener.onItemCompontClickListener(9, Constant.BlogItemCompontType.SCAN_BIG_IMAGE, position);
	            }
			}
		});
        return view;
    }

//    private void bindData(int i, BlogAdapterViewHolder blogAdapterViewHolder, JsonMap jsonMap) {
//        BlogPicAdapter blogPicAdapter = new BlogPicAdapter(context);
//        blogAdapterViewHolder.blog_content_image_gv.setAdapter(blogPicAdapter);
//        blogAdapterViewHolder.blog_content_image_gv.setVisibility(View.VISIBLE);
//    }
    private void bindData(int i, BlogAdapterViewHolder blogAdapterViewHolder, JsonMap jsonMap,Moment moment) {
    	NetworkImageAdapter blogPicAdapter = new NetworkImageAdapter(context, Arrays.asList(moment.address));
        blogAdapterViewHolder.blog_content_image_gv.setAdapter(blogPicAdapter);
        blogAdapterViewHolder.blog_content_image_gv.setVisibility(View.VISIBLE);
    }

    private void setTag(int i, BlogAdapterViewHolder blogAdapterViewHolder) {
        blogAdapterViewHolder.attention_option_tv.setTag(i);
        blogAdapterViewHolder.blog_user_photo_iv.setTag(i);
        blogAdapterViewHolder.attention_tv.setTag(i);
        blogAdapterViewHolder.attentioned_option_iv.setTag(i);
        blogAdapterViewHolder.blog_content_image_gv.setTag(i);
        blogAdapterViewHolder.transmit_layout.setTag(i);
        blogAdapterViewHolder.comment_layout.setTag(i);
    }


    private class BlogAdapterViewHolder {
        @ViewInject(R.id.blog_recommend_title_layout)
        private LinearLayout blog_recommend_title_layout;
        @ViewInject(R.id.attention_option_tv)
        private TextView attention_option_tv;

        /**
         * 推荐日志 选项操作
         *
         * @param view
         */
        @OnClick(R.id.attention_option_tv)
        public void attention_option_tv_click(View view) {
            if (itemCompontClickListener != null) {
                itemCompontClickListener.onItemCompontClickListener((Integer) view.getTag(), Constant.BlogItemCompontType.RECOMMAND_OPTION, -1);
            }
        }

        @ViewInject(R.id.blog_user_photo_iv)
        private CircleImageView blog_user_photo_iv;

        @OnClick(R.id.blog_user_photo_iv)
        public void user_photo_iv_click(View view) {
            if (itemCompontClickListener != null) {
                itemCompontClickListener.onItemCompontClickListener((Integer) view.getTag(), Constant.BlogItemCompontType.USER_PHOTO, -1);
            }
        }

        @ViewInject(R.id.userName_tv)
        private TextView userName_tv;

        @ViewInject(R.id.create_time_tv)
        private TextView create_time_tv;


        @ViewInject(R.id.attention_tv)
        private TextView attention_tv;

        @OnClick(R.id.attention_tv)
        public void attention_tv_click(View view) {
            if (itemCompontClickListener != null) {
                itemCompontClickListener.onItemCompontClickListener((Integer) view.getTag(), Constant.BlogItemCompontType.ATTENTION, -1);
            }
        }

        @ViewInject(R.id.attentioned_option_iv)
        private ImageView attentioned_option_iv;

        @OnClick(R.id.attentioned_option_iv)
        public void attentioned_option_iv_click(View view) {
            if (itemCompontClickListener != null) {
                itemCompontClickListener.onItemCompontClickListener((Integer) view.getTag(), Constant.BlogItemCompontType.ATTENTIONED_OPTION, -1);
            }
        }

        @ViewInject(R.id.blog_content_txt_tv)
        private TextView blog_content_txt_tv;

        @ViewInject(R.id.blog_content_image_gv)
        private NineGridView blog_content_image_gv;

//        @OnItemClick(R.id.blog_content_image_gv)
//        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        	Toast.makeText(context, "onItemClick", Toast.LENGTH_SHORT).show();
//            if (itemCompontClickListener != null) {
//                itemCompontClickListener.onItemCompontClickListener((Integer) parent.getTag(), Constant.BlogItemCompontType.SCAN_BIG_IMAGE, position);
//            }
//        }

        @ViewInject(R.id.transmit_layout)
        private LinearLayout transmit_layout;

        @OnClick(R.id.transmit_layout)
        public void transmit_layout_click(View view) {
            if (itemCompontClickListener != null) {
                itemCompontClickListener.onItemCompontClickListener((Integer) view.getTag(), Constant.BlogItemCompontType.TRANSMIT, -1);
            }
        }

        @ViewInject(R.id.comment_layout)
        private LinearLayout comment_layout;

        @OnClick(R.id.comment_layout)
        public void comment_layout_click(View view) {
            if (itemCompontClickListener != null) {
                itemCompontClickListener.onItemCompontClickListener((Integer) view.getTag(), Constant.BlogItemCompontType.COMMENT, -1);
            }
        }

    }

    public interface ItemCompontClickListener {
        /**
         * @param position         根位置
         * @param shopCartitemType 类别
         * @param index            点击具体的商品位置
         */
        void onItemCompontClickListener(int position, int shopCartitemType, int index);
    }

    static class Moment {
        public String content ;
        public Integer[] resource ; //
        public String[] address ;
    }

}
